﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using PCSComUtils.Common;
using PCSUtils.Utils;
using PCSComUtils.PCSExc;
using PCSUtils.Log;
using PCSComPayroll.BO;

namespace PCSReport.Payroll
{
    public partial class BaoCaoDinhMuc : Form
    {
        public BaoCaoDinhMuc()
        {
            InitializeComponent();
        }

        private void BaoCaoDinhMuc_Load(object sender, EventArgs e)
        {

        }

        private void btnCode_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = ".btnFGoodsCode_Click()";
            try
            {

                DataRowView drwResult = null;
                drwResult = FormControlComponents.OpenSearchForm(ITM_ProductTable.TABLE_NAME, ITM_ProductTable.CODE_FLD, txtFoodsCode.Text.Trim(), null, true);
                if (drwResult != null)
                {
                    txtFoodsCode.Tag = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    txtFoodsCode.Text = drwResult[ITM_ProductTable.CODE_FLD].ToString();
                    txtPartName.Text = drwResult[ITM_ProductTable.DESCRIPTION_FLD].ToString();
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void btnName_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = ".btnFGoodsCode_Click()";
            try
            {

                DataRowView drwResult = null;
                drwResult = FormControlComponents.OpenSearchForm(ITM_ProductTable.TABLE_NAME, ITM_ProductTable.DESCRIPTION_FLD, txtPartName.Text.Trim(), null, true);
                if (drwResult != null)
                {
                    lblFgoodsCode.Tag = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    txtFoodsCode.Text = drwResult[ITM_ProductTable.CODE_FLD].ToString();
                    txtPartName.Text = drwResult[ITM_ProductTable.DESCRIPTION_FLD].ToString();
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            ExecuteReport objReport = new ExecuteReport();
            PayrollReportBO obj = new PayrollReportBO();
            DataSet dset = new DataSet();
            if (txtFoodsCode.Tag != null)
            {
                DataTable dtb = obj.ExecuteReport(txtFoodsCode.Tag.ToString());
                dset.Tables.Add(dtb);
            }
            List<object> listParas = new List<object>();
            List<object> listValues = new List<object>();
            listParas.Add("fldPartParam");
            listValues.Add(txtFoodsCode.Text.Trim());
            listParas.Add("fldPartNameParam");
            listValues.Add(txtPartName.Text.Trim());
            objReport.showReport(dset, "BaoCaoDinhMuc.xml", listParas, listValues);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
